package com.proj;

import java.util.*;
import java.util.stream.Collectors;

public class SimpleTagDb implements TagInterface {
    private List<TagDao> tags = new ArrayList<>();
    private int next_id = 1;

    @Override
    public Tag createTag(String name, String detail) {
        TagDao tag = new TagDao("tag-" + next_id, name, detail);
        tags.add(tag);
        next_id += 1;
        return tag;
    }

    @Override
    public Collection<Tag> getTagById(Collection<String> ids) {
        Set<String> take = new HashSet<>(ids);
        return tags.stream().filter(t -> take.contains(t.getId())).collect(Collectors.toList());
    }

    @Override
    public Tag getTagByName(String name) {
        return tags.stream().filter(t -> t.getName().equals(name)).findFirst().orElse(null);
    }

    @Override
    public void deleteTagById(String id) {
        tags = tags.stream().filter(t -> !t.getId().equals(id)).collect(Collectors.toList());
    }

    @Override
    public void updateTag(Tag tag) {
        tags.stream().filter(t -> t.getId().equals(tag.getId())).forEach(t -> {
            t.setName(tag.getName());
            t.setDetails(tag.getDetails());
        });
    }

    @Override
    public Collection<Tag> fuzzySearch(String q, int limit) {
        return tags.stream().filter(t -> t.getName().matches(q) || t.getDetails().matches(q)).collect(Collectors.toList());
    }
}
